package com.haidechizi.fastadmin.modules.code.controller;

import com.haidechizi.fastadmin.common.page.PageDto;
import com.haidechizi.fastadmin.common.utils.PageInfo;
import com.haidechizi.fastadmin.common.utils.Response;
import com.haidechizi.fastadmin.common.validator.ValidatorUtils;
import com.haidechizi.fastadmin.common.base.AbstractController;
import com.haidechizi.fastadmin.modules.code.entity.PropCodeConfig;
import com.haidechizi.fastadmin.modules.code.service.PropCodeConfigService;
import com.haidechizi.fastadmin.modules.sys.entity.SysDict;
import com.haidechizi.fastadmin.modules.sys.service.SysDictService;
import java.util.List;
import java.util.Map;
import com.haidechizi.fastadmin.common.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import com.google.common.collect.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Arrays;


/**
 * 资源文件Controller
 * @author wangdejun
 * @version 2020-11-05
 */
@RestController
@RequestMapping(value = "code/propCodeConfig")
public class PropCodeConfigController extends AbstractController {

	@Autowired
	private PropCodeConfigService propCodeConfigService;

	@Autowired
    private SysDictService sysDictService;

	/**
     * 资源文件列表
     */
	@RequiresPermissions("code:propCodeConfig:list")
	@RequestMapping(value = "list")
	public Response list(PageDto pageDto, PropCodeConfig propCodeConfig) {
		PageInfo page = propCodeConfigService.findPage(pageDto, propCodeConfig);
        return Response.ok().put("page", page);
	}

	/**
     * 资源文件信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("code:propCodeConfig:info")
    public Response info(@PathVariable("id") Long id) {
        PropCodeConfig propCodeConfig = propCodeConfigService.selectById(id);
		if (StringUtils.isNotBlank(propCodeConfig.getLanguageType())) {
            propCodeConfig.setLanguageTypeList(Lists.newArrayList(StringUtils.split(propCodeConfig.getLanguageType(), ",")));
        }
        return Response.ok().put("propCodeConfig", propCodeConfig);
    }

	/**
	* 资源文件保存
	*/
	@RequiresPermissions("code:propCodeConfig:save")
	@RequestMapping(value = "save")
	public Response save(@RequestBody PropCodeConfig propCodeConfig) {
		if (CollectionUtils.isNotEmpty(propCodeConfig.getLanguageTypeList())) {
            propCodeConfig.setLanguageType(StringUtils.join(propCodeConfig.getLanguageTypeList(), ","));
        }
        //校验类型
        ValidatorUtils.validateEntity(propCodeConfig);
        propCodeConfigService.insert(propCodeConfig);
        return Response.ok();
    }

    /**
     * 资源文件修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("code:propCodeConfig:update")
    public Response update(@RequestBody PropCodeConfig propCodeConfig) {
		if (CollectionUtils.isNotEmpty(propCodeConfig.getLanguageTypeList())) {
            propCodeConfig.setLanguageType(StringUtils.join(propCodeConfig.getLanguageTypeList(), ","));
        }
        //校验类型
        ValidatorUtils.validateEntity(propCodeConfig);
        propCodeConfigService.updateById(propCodeConfig);
        return Response.ok();
    }


	/**
     * 资源文件删除
     */
	@RequiresPermissions("code:propCodeConfig:delete")
	@RequestMapping(value = "delete")
	public Response delete(@RequestBody Long[] ids) {
		propCodeConfigService.deleteBatchIds(Arrays.asList(ids));
        return Response.ok();
	}

    /**
     * 获取字典列表(初始化select、checkbox、radio)
     */
    @RequiresPermissions("code:propCodeConfig:list")
    @RequestMapping(value = "dictConfig")
    public Response dictConfig() {
		List<SysDict> generatorTypeList = sysDictService.queryDictByType("generator_type");
		List<SysDict> languageTypeListList = sysDictService.queryDictByType("");
		List<SysDict> translateTypeList = sysDictService.queryDictByType("translate_type");

        return Response.ok()
				.put("generatorTypeList", generatorTypeList)
				.put("languageTypeListList", languageTypeListList)
				.put("translateTypeList", translateTypeList)

    	;
    }


    /**
     * 获取字典Map(初始化字典数据，将code转成value)
     */
    @RequiresPermissions("code:propCodeConfig:list")
    @RequestMapping(value = "dictMap")
    public Response dictMap() {
		Map<String,String> generatorTypeMap = sysDictService.queryDictMapByType("generator_type");
		Map<String,String> languageTypeListMap = sysDictService.queryDictMapByType("");
		Map<String,String> translateTypeMap = sysDictService.queryDictMapByType("translate_type");

        return Response.ok()
				.put("generatorTypeMap", generatorTypeMap)
				.put("languageTypeListMap", languageTypeListMap)
				.put("translateTypeMap", translateTypeMap)

		;
    }

}